#import "MapSearchUtility.h"

static MapSearchUtility* sharedMapSearchUtility = nil;

/**
 * Provides an interface in between the Map subsystem and the event subsystem.
 * 
 * Allows for the interaction between responses and requests 
 *
 * @author      Justin Hollander
 * @version     0.5.0
 * @since       0.3.0
 * @see         SFUEventSearchEngineDelegate
 */
@implementation MapSearchUtility

/**
 * Returns shared map search Utility object 
 *
 * @return  MapSearchUtility reference
 */
+(MapSearchUtility*) sharedMapSearchUtility{
	@synchronized(self) {
		if (sharedMapSearchUtility == nil) {
			sharedMapSearchUtility = [[MapSearchUtility alloc] init];
		}
	}
	
	return sharedMapSearchUtility;
}



/**
 * Attempts to match search parameters to a building. If a match is found the 
 * corresponding building is returned.
 *
 * @param searchText search parameter that contains building name keyword
 * @return matching buiding or null if no match
 */
-(Building*) searchByBuilding:(NSString*)searchText{
	
		if (searchText != nil) {
			return [ParsingUtilities parseBuildingFromString:searchText];
		};

	return nil;
}

/**
 * Keyword event search.
 *
 * @param searchText
 *
 */
-(void) searchByEvent:(NSString*)searchText{
	EventManager* em = [EventManager sharedEventManager];
	[em setSearchDelegate:self];
	if(em == nil) return;
	
	if( searchText != nil )
		[em searchLocEventsByKeywordString:searchText 
											 matchAllKeywords:NO 
												  hasLocation:YES];
}

/**
 * Sets the delegate for the event manager and calls the event managers
 * search events. This leads to the viewing of the EventSearchViewer
 * 
 * @param navController main application navigation controller 
 * @param delegate      delegate for for event manager
 */
-(void)searchEventsWithNavController:(UINavigationController*)navController 
					  searchDelegate:(id<SFUEventSearchResponseDelegate>)delegate {
	eventSearchDelegate = delegate;
	[EventManager searchEventsWithNavController:navController 
													  searchDelegate:self];
	
}

/**
 * Sets the delegate for this class
 * 
 * @param  delegate for this class
 */
-(void) setDelegate:(id<SFUEventSearchResponseDelegate>)delegate{
	
		eventSearchDelegate = delegate;
	
}

/**
 * Takes a category list and searches for points of interests.
 * 
 */
-(NSArray*) searchByPOI:(NSArray*)categoryList{	
	
		NSArray* POIMatchArray = [[NSArray alloc] init];
		PointOfInterestCollection* poic = [PointOfInterestCollection sharedPointOfInterestCollection];
		
		if ((categoryList != nil) && (poic != nil))
			for(NSString* poiCategory in categoryList)
				[POIMatchArray arrayByAddingObjectsFromArray:
				 [poic findDataTypesWithKeywordString:poiCategory matchAllKeywords:NO]];
		
		return POIMatchArray;
}

/**
 * Delegates a failed search to the map view controller.
 * 
 * @param  theConnection  contians all url connection specific elements
 * @param  withError      error message
 */
- (void)searchDidFail:(NSURLConnection*)theConnection withError:(NSString*)message {
	
		if (eventSearchDelegate != nil) {
			[eventSearchDelegate eventResponseBySearchFailure:message];
		}
	
	return;
}

/**
 * on successful search this method delegates the response to the map view
 * controller
 *
 * @param theConnection  contians all url connection specific elements
 * @param eventResults   the set of matched events
 */
- (void)searchDidFinish:(NSURLConnection*)theConnection 
				results:(NSArray*)eventResults{	
	
	if (eventSearchDelegate != nil) {
		[eventSearchDelegate eventResponseBySearchSuccess:eventResults];
	}

	return;
}


@end
